$(function(){
	list();
})

function list(){
	var arr = {
		hf_s_id: 'WD11',
		hf_srv_id: 'Cv_jcus_cc',
		hs_action_id: 'get_list'
	}
	getAjaxRequest(arr,function(result){
		console.log(result)
		var htmllist = '';
		var len = result.length;
		if( len > 0 ){
			for (var i = 0; i<len; i++) {
				htmllist += '<div class="item">'+
							'<div class="name" data-key="'+result[i].bj_cc_key+'" data-card="'+(i+1)+'">'+
							'Card '+(i+1)+

							'<span class="mui-pull-right del" ><img src="'+MPJ.base+'/resources/images/account/delete.png" /></span>'+
							'</div>'+
							'<div class="content">'+
							'<img src="'+MPJ.base+'/resources/images/visa.png" />'+
							'<div class="main">'+
							'<h4>Visa</h4>'+
							'<p>'+result[i].bs_cc_mask_no+'</p>'+
							'</div>'+
							'</div>'+
							'</div>';
			}
			$('.listBox').html(htmllist);
			del();
		}else {
			$('.listBox').html(no_result);
		}
	})
}


//删除
function del() {
	$('.listBox .item').each(function (index) {
		var $this = $(this);
		var $close = $(this).find('.del');
		var key = $(this).find('.name').data('key');
		var card = $(this).find('.name').data('card');
		$close.on('tap', function () {
			dialog('Delete credit card information', 'Are you sure want to delete “'+card+' ”from your credit card list?', 3);
			var $dialog = $('#dialog');
			$dialog.show();
			$dialog.find('.comfirmBtn').one('click', function () {

				delCard(key);
				$dialog.hide();
				$dialog.remove();
				$this.remove();
			});
		})
	})
}
function delCard(key){
	var arr = {
		hf_s_id: 'WD11',
		hf_srv_id: 'Cv_jcus_cc',
		hs_action_id: 'delete',
		hs_cc_key:key
	}
	getAjaxRequest(arr,function(result){
		mui.toast("Success");
		closeToast();
	})
}



$('.add-credit-card  a.mui-btn').on('tap',function(){
	location.href=MPJ.base+"/pages/account/credit/add.jsp";
})
function closeToast(){
	setTimeout(function(){
		$('.mui-toast-container').remove();
	},1000)
}

$('.save').on('tap',function(){
	save();
})